use serde::Serialize;
use std::{
    ops::{Deref, DerefMut},
    sync::Mutex,
};

use crate::storage::Storage;

pub struct SharedData {
    pub storage: Mutex<Storage>,
}

impl Deref for SharedData {
    type Target = Mutex<Storage>;
    fn deref(&self) -> &Self::Target {
        &self.storage
    }
}

impl DerefMut for SharedData {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.storage
    }
}

impl Default for SharedData {
    fn default() -> Self {
        SharedData {
            storage: Mutex::new(Storage::default()),
        }
    }
}

#[derive(Debug, Serialize)]
pub struct Response<T: Serialize> {
    code: u32,
    message: &'static str,
    data: Option<T>,
}

impl<T: Serialize> Response<T> {
    pub fn new(code: u32, message: &'static str, data: Option<T>) -> Self {
        Response {
            code,
            message,
            data,
        }
    }
}

#[derive(Serialize)]
pub struct DevResponse {
    status: bool,
}

impl DevResponse {
    pub fn success() -> Self {
        DevResponse { status: true }
    }

    pub fn failure() -> Self {
        DevResponse { status: false }
    }
}

#[derive(Debug, PartialEq, Eq)]
pub enum Error {
    NotExisted,
    Exist,
    Occupied,
    Vacant,
}
